package mo.qmul.gemom;

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.qsphere.boundary.gemom.GEMOMBrokerAdapter;
import com.qsphere.boundary.gemom.GEMOMClientData;
import com.qsphere.boundary.gemom.GEMOMData;
import com.qsphere.boundary.gemom.GEMOMListener;
import com.qsphere.boundary.gemom.GEMOMMessageType;
import com.qsphere.boundary.gemom.GEMOMParameters;
import com.qsphere.boundary.gemom.impl.GEMOMBrokerAdapterImpl;
/** COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 * */
public class publisher extends JFrame implements ActionListener, GEMOMListener{
	
	private static final String CONNECT = "connect";
	private static final String PUBLISH = "publish";
	private static final String DISCONNECT = "disconnect";
	
	protected static final String VOLUMETRY_TEST_TOPIC = "__FAST1";
	protected boolean m_showVolumetryMonitorMessages = true;
	protected int m_updateCounter = 0;
	protected int m_volumetryTestPublishCount = 100;



	protected TextField ipstring= new TextField();
	protected TextField portstring= new TextField();
	protected TextField m_topicField = new TextField();
	protected TextField m_namespaceField = new TextField();
	
	protected JTable mTable;
	protected JScrollPane mscroll = new JScrollPane();

	protected GEMOMBrokerAdapter m_brokerInterface = null;
	private boolean m_isConnected = false;


	private Button connectButton;
	private Button disconnectButton;
	private Button publishButton;
	
	private String[] field;
	private String[] values;
	
	public publisher(String[] f, String[] v){
		setTitle("Connection Control");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		field=f;
		values=v;

		initialiseStartupParameters();
		
		
		
	}
	
	protected void initialiseStartupParameters() {
		// set up the connection panel
		JPanel conPanel= new JPanel(new GridLayout(3, 3));

		Label l1 = new Label("IP Address", Label.CENTER);
		Label l2= new Label("Port", Label.CENTER);

		conPanel.add(l1);
		conPanel.add(ipstring);

		conPanel.add(l2);
		conPanel.add(portstring);

		connectButton = new Button("Connect");
		connectButton.setActionCommand(CONNECT);
		connectButton.addActionListener(this);
		conPanel.add(connectButton);

		disconnectButton = new Button("Disconnect");
		disconnectButton.setActionCommand(DISCONNECT);
		disconnectButton.addActionListener(this);
		conPanel.add(disconnectButton);

		getContentPane().add(conPanel,"North");
		
		// set up the publish panel
		JPanel sPanel= new JPanel(new GridLayout(1, 3));
		
		
		sPanel.add(m_namespaceField);
		sPanel.add(m_topicField);		

		publishButton = new Button("Publish");
		publishButton.setActionCommand(PUBLISH);
		publishButton.addActionListener(this);
		sPanel.add(publishButton);

		getContentPane().add(sPanel,"South");
		
		
		// set up the solution panel
		String[] colName={"Gbroker","Re-allocation"};
		DefaultTableModel model= new DefaultTableModel(colName,0);
		
		
		for(int i=0; i< field.length; i++)
		{
			Object[] rowData= new Object[2];
			
			rowData[0]=field[i];
			rowData[1]=values[i];
			
			model.addRow(rowData);
		}

		mTable= new JTable(model);
		mscroll.getViewport().add(mTable);
		
		getContentPane().add(mscroll,"Center");
		
		
		this.setBounds(10, 10, 500, 400);

	}
	private void onConnect() {

		GEMOMParameters connectParameters = new GEMOMParameters();

		String ip= ipstring.getText();
		String port= portstring.getText();

		connectParameters.put("ConnectionString", "client@"+ip+":"+port+"\n");



		if (getGEMOMBrokerInterface().connect("TestinJNI", null,
				connectParameters, null, null)) {
			setIsConnected(true);

		} else {
			JOptionPane.showMessageDialog(this, "Can't connect", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void onDisconnect() {
		if (getGEMOMBrokerInterface().disconnect(null, null, null)) {
			setIsConnected(false);

		} else {
			JOptionPane.showMessageDialog(this, "Can't disconnect", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void setIsConnected(boolean isConnected) {
		m_isConnected = isConnected;
		connectButton.setEnabled(!isConnected);
	}
	
	protected GEMOMBrokerAdapter getGEMOMBrokerInterface() {
		if (m_brokerInterface == null) {

			m_brokerInterface = new GEMOMBrokerAdapterImpl();
			GEMOMParameters connectParameters = new GEMOMParameters();
			// connectParameters.put("ConnectionString",
			// "client@127.0.0.1:7891");

			// m_brokerInterface.connect("TestinJNI", null, connectParameters,
			// null, null);
		}
		return m_brokerInterface;
	}
	
	
	/**
	 * Performs publish of data entered by user
	 */
	protected void onPublish() {
		
		String namespace = m_namespaceField.getText();
		String topic = m_topicField.getText();
		if (topic == null || topic.equalsIgnoreCase("")) {
			return;
		}
		if (VOLUMETRY_TEST_TOPIC.equalsIgnoreCase(topic)) {
			
		} else {
			GEMOMData data = new GEMOMData();
			for (int i = 0; i < field.length; i++) {
				String fieldName = field[i];
				String fieldValue = values[i];
				data.put(fieldName, fieldValue);
			}
			// getGEMOMBrokerInterface().publish(this, topic, data, null);
			getGEMOMBrokerInterface().publish(null, namespace, topic, data,
					null, null);

		}
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String command = e.getActionCommand();
		//debug("Action command: " + command + " isNamespace subscribe: "
		//		+ NAMESPACE_SUBSCRIBE.equals(command));
		if (PUBLISH.equalsIgnoreCase(command)) {
			onPublish();
		} else if (CONNECT.equalsIgnoreCase(command)) {
			onConnect();
		} else if (DISCONNECT.equalsIgnoreCase(command)) {
			onDisconnect();
		} 
	}

	@Override
	public String getGEMOMClientName() {
		// TODO Auto-generated method stub
		return "Resilience Manager";
	}

	@Override
	public boolean update(String arg0, String arg1, GEMOMMessageType arg2,
			GEMOMData arg3, GEMOMClientData arg4) {
		// TODO Auto-generated method stub
		// no need update for topic
		return true;
	}

	@Override
	public boolean updateNamespace(String arg0, String arg1, String arg2,
			GEMOMMessageType arg3, GEMOMClientData arg4) {
		// TODO Auto-generated method stub
		// no need update for namespace
		return true;
	}
	
	

}
